package com.hao.tool.extra.simple;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HtmlUtil;
import com.hao.tool.common.util.HttpClientUtil;
import org.apache.commons.text.StringEscapeUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;

/**
 * description
 *
 * @author HaoZ on CMHK.hainan
 * @date 2021/6/1 上午9:08
 */
public class HtmlUrlClearTool {

    public static void main(String[] args) throws IOException {
        HtmlUrlClearTool htmlUrlClearTool = new HtmlUrlClearTool();
        htmlUrlClearTool.execute();
    }


    public void execute() throws IOException {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入：【需去除HTML标签的URL】");
        String urlPath = scan.next();
        System.out.println("信息处理中，请稍等······");

        String content = HttpClientUtil.build().setLogStatus(true)
                .setChartStr("gb2312")
                .addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36")
                .sendGet(urlPath, new HashMap<>());


        content = HtmlUtil.cleanHtmlTag(content);
        content = HtmlUtil.unescape(content);
        content = StringEscapeUtils.unescapeJava(content);
        System.out.println(content);

        String dateStr = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN);
        File textFile = new File("HaoSimpleTool" + File.separator + dateStr + ".txt");
        FileUtil.writeString(content, textFile, Charset.defaultCharset());

        System.out.println("---------- 信息已处理，请查看 ----------");
        System.out.println(textFile.getAbsolutePath());
    }
}

